<?php
/**
 * Talents come from diligence, and knowledge is gained by accumulation.
 *
 * @author: 晋<657306123@qq.com>
 */
declare (strict_types = 1);

namespace app;

use app\common\service\SafeHandle;

/**
 * Class Request
 *
 * @package app
 */
class Request extends \think\Request{

	/**
	 * @var static
	 */
	private static $INSTANCE;

	/**
	 * 数据源
	 *
	 * @var array
	 */
	protected $data = [];

	/**
	 * 插件
	 *
	 * @var string
	 */
	protected $plugin;

	/**
	 * Request constructor.
	 */
	public function __construct(){
		parent::__construct();
		self::$INSTANCE = $this;
	}

	/**
	 * 获取页码
	 *
	 * @return int
	 */
	public function getPage():int{
		if(!isset($this->data['page'])){
			$page = $this->param('page/d', 0);
			$this->data['page'] = $page < 1 ? 1 : $page;
		}
		return $this->data['page'];
	}

	/**
	 * 获取分页条数
	 *
	 * @param int $max
	 * @param int $default
	 * @return int
	 */
	public function getLimit(int $max = 100, int $default = 20):int{
		if(!isset($this->data['limit'])){
			$limit = $this->param('limit/d', 0);
			if($limit < 1){
				$this->data['limit'] = $default;
			}else{
				$this->data['limit'] = $limit > $max ? $max : $limit;
			}
		}
		return $this->data['limit'];
	}

	/**
	 * 获取记录偏移数
	 *
	 * @return int
	 */
	public function getOffset():int{
		if(!isset($this->data['offset'])){
			$offset = $this->param('offset/d', 0);
			$this->data['offset'] = $offset < 1 ? 1 : $offset;
		}
		return $this->data['offset'];
	}

	/**
	 * 获取排序的字段
	 *
	 * @return string
	 */
	public function getSort():string{
		// todo 重新优化
		$sort = isset($_GET['sort']) ? trim($_GET['sort']) : '';
		return $sort;
	}

	/**
	 * 获取范围时间
	 *
	 * @param string $field
	 * @param int    $maxRange
	 * @param string $delimiter
	 * @return array
	 */
	public function getRangeTime(string $field = 'datetime', int $maxRange = 0, string $delimiter = ' - '):array{
		$rangeTime = $this->param($field, '');
		return parse_range_datetime($rangeTime, $maxRange, $delimiter);
	}

	/**
	 * 获取筛选关键字
	 *
	 * @param string $field
	 * @return string
	 */
	public function getKeywords(string $field = 'keywords'):string{
		$key = 'keywords_'.$field;
		if(!isset($this->data[$key])){
			if($this->has($field, 'get')){
				$keywords = $this->get($field);
			}else{
				$keywords = $this->post($field, '');
			}
			$keywords = trim($keywords);
			$this->data[$key] = $keywords;
		}
		return $this->data[$key];
	}

	/**
	 *  获取关键字SQL
	 *
	 * @param string $field
	 * @return array
	 */
	public function getKeywordsSql(string $field = 'keywords'):array{
		return build_keyword_sql($this->getKeywords($field));
	}

	/**
	 * 设置当前的插件名
	 *
	 * @access public
	 * @param string $plugin 插件名
	 * @return $this
	 */
	public function setPlugin(string $plugin):self{
		$this->plugin = $plugin;
		return $this;
	}

	/**
	 * 获取当前的模块名
	 *
	 * @access public
	 * @return string
	 */
	public function plugin():string{
		return $this->plugin ?: '';
	}

	/**
	 * 获取ID list
	 *
	 * @param string $field
	 * @return array
	 */
	public function getIds($field = 'ids'):array{
		$ids = $this->param($field);
		return SafeHandle::idStringToArray($ids[$field]);
	}

	/**
	 * 设置 数据
	 *
	 * @param string $key
	 * @param mixed  $value
	 */
	public function setOption(string $key, &$value){
		$this->data[$key] = $value;
	}

	/**
	 * 获取数据
	 *
	 * @param string $key
	 * @return mixed
	 */
	public function &getOption(string $key){
		return $this->data[$key];
	}

	/**
	 * 获取当前请求平台
	 *
	 * @return string
	 */
	public static function getRequestPlatform():string{
		static $platform = null;
		if(is_null($platform)){
			$request = \think\facade\Request::instance();
			$userAgent = $request->server('HTTP_USER_AGENT');
			$referer = $request->server('HTTP_REFERER');
			if(stripos($userAgent, "wechatdevtools") !== false
				|| stripos($userAgent, "MicroMessenger") !== false
				|| stripos($referer, "servicewechat.com") !== false){
				$platform = 'wechat';
			}elseif(stripos($userAgent, "AliApp") !== false
				|| stripos($userAgent, "Alipay") !== false
				|| stripos($userAgent, "AlipayDefined") !== false
				|| stripos($userAgent, "AlipayClient") !== false
				|| stripos($userAgent, "支付宝") !== false
				|| stripos(urldecode($userAgent), "支付宝") !== false){
				$platform = 'alipay';
			}elseif(stripos($userAgent, "swandevtools") !== false
				|| stripos($userAgent, "baiduboxapp") !== false
				|| stripos($userAgent, "baiduboxapp") !== false
				|| stripos($referer, "smartapp.baidu.com") !== false){
				$platform = 'baidu';
			}else{
				$platform = 'browser';
			}
		}
		return $platform;
	}

	/**
	 * 是否是微信请求
	 *
	 * @return bool
	 */
	public static function isWechat():bool{
		return "wechat" === self::getRequestPlatform();
	}

	/**
	 * 是否是支付宝请求
	 *
	 * @return bool
	 */
	public static function isAlipay():bool{
		return "alipay" === self::getRequestPlatform();
	}

	/**
	 * 是否是百度请求
	 *
	 * @return bool
	 */
	public static function isBaidu():bool{
		return "baidu" === self::getRequestPlatform();
	}

	/**
	 * 是否是浏览器请求
	 *
	 * @return bool
	 */
	public static function isBrowser():bool{
		return "browser" === self::getRequestPlatform();
	}

}
